﻿using SanJ.FreeCare.ObservationDefinitions;
using SanJ.FreeCare.Peis.Configure.ProjectPage;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace SanJ.FreeCare.Peis
{
    public partial class PeisAppService : FreeCareAppService, IPeisAppService
    {
        public async Task<ProjectDto> GetProjectAsync(Guid id)
        {
            var pro = await _observationDefinitionManager.GetByIdAsync(id);
            return ToProjectDto(pro);
        }

        public async Task<PagedResultDto<ProjectDto>> GetProjectListAsync(GetProjectInput input)
        {
            var totalCount = await _observationDefinitionManager.GetCountAsync(input.FilterText);
            var items = await _observationDefinitionManager.GetPageLisAsync(input.FilterText, input.Sorting, input.SkipCount, input.MaxResultCount);
            List<ProjectDto> project = new List<ProjectDto>();
            return new PagedResultDto<ProjectDto>
            {
                TotalCount = totalCount,
                Items = project,
            };
        }

        public async Task<ProjectDto> CreateProjectAsync(ProjectCreateDto input)
        {
            ObservationDefinition odi = ToObservationDefinitionAsync(input);
            var insert = await _observationDefinitionManager.CreateAsync(odi);
            ProjectDto result = new ProjectDto();
            return result;
        }

        private ObservationDefinition ToObservationDefinitionAsync(ProjectCreateDto input)
        {
            ObservationDefinition odi = new ObservationDefinition();
            odi.Spell = input.Spell;
            odi.Name = input.Name;
            odi.DisplayOrder = input.DisplayOrder;
            odi.Category= input.Category;
            odi.PermittedDataType = input.DataType;
            return odi;
        }

        public async Task<ProjectDto> UpdateProjectAsync(ProjectUpdateDto input)
        {
            ObservationDefinition odi = ToObservationDefinitionAsync(input);
            var updateEntity = await _observationDefinitionManager.UpdateAsync(input.Id,odi);
            var result = ToProjectDto(odi);
            return result;
        }

        private ObservationDefinition ToObservationDefinitionAsync(ProjectUpdateDto input)
        {
            throw new NotImplementedException();
        }

        public Task DeleteProjectAsync(Guid id)
        {
            return _observationDefinitionManager.Delete(id);
        }
        private ProjectDto ToProjectDto(ObservationDefinition odi)
        {
            ProjectDto project = new ProjectDto();
            project.Id = odi.Id;
            project.Name = odi.Name;
            project.Description = odi.Description;
            project.Spell = odi.Spell;
            project.PermittedDataType = odi.PermittedDataType;
            project.Category = odi.Category;
            project.PermittedUnit = odi.PermittedUnit;
            project.PreferredReportName = odi.PreferredReportName;
            project.DisplayOrder = odi.DisplayOrder;
            project.Note = odi.Note;
            project.PreferentialPrice = odi.PreferentialPrice;
            project.Gerden = odi.Gerden;
            project.OriginalePrice = odi.OriginalePrice;
            return project;
        }
    }
}
